﻿@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@

@{
    Layout = "~/Views/Shared/__ComponentLayout.cshtml";
    string editId = ViewData.Count >0 ? ViewData["0"].ToString() : "null";
    string randomId = Guid.NewGuid().ToString();

}
@using ST.Torch.WebApi.Web.Core;
@inherits UtilRazorPage<object>

<form style="padding: 16px;padding-right:30px"  class="layui-form" action=""  lay-filter="edit-data_@randomId">
     <input type="text" name="Id"  placeholder="" autocomplete="off" class="layui-input" style="display:none">
    
         <div class="layui-form-item">
              <label class="layui-form-label">@L("用户OpenId")</label>
              <div class="layui-input-block">
                 <input type="text" name="OpenId" required  lay-verify="required"  autocomplete="off" class="layui-input">
              </div>
         </div>
         <div class="layui-form-item">
              <label class="layui-form-label">@L("公司代码")</label>
              <div class="layui-input-block">
                 <input type="text" name="C_Code" required  lay-verify="required"  autocomplete="off" class="layui-input">
              </div>
         </div>
         <div class="layui-form-item">
              <label class="layui-form-label">@L("公司名称")</label>
              <div class="layui-input-block">
                 <input type="text" name="C_Name" required  lay-verify="required"  autocomplete="off" class="layui-input">
              </div>
         </div>
         <div class="layui-form-item">
              <label class="layui-form-label">@L("获奖礼物名称")</label>
              <div class="layui-input-block">
                 <input type="text" name="G_Name" required  lay-verify="required"  autocomplete="off" class="layui-input">
              </div>
         </div>
         <div class="layui-form-item">
              <label class="layui-form-label">@L("获奖礼物名称")</label>
              <div class="layui-input-block">
                 
                <input type="text" style="display:none" id="G_ImgUrlInputEle_@randomId" name="G_ImgUrl" placeholder="" autocomplete="off" class="layui-input">
                <div class="layui-upload">
                    <button type="button" class="layui-btn" id="G_ImgUrlButtonEle_@randomId">@L("上传图片")</button>
                    <div class="layui-upload-list">
                        <img class="layui-upload-img" id="G_ImgUrlEle_@randomId" style="height:50px;width:50px">
                        <p id="G_ImgUrlText_@randomId"></p>
                    </div>
                    <div style="width: 95px;" id="G_ImgUrlProgressEle_@randomId">
                        <div class="layui-progress layui-progress-big" lay-showpercent="yes" lay-filter="G_ImgUrlProgress_@randomId">
                            <div class="layui-progress-bar" lay-percent=""></div>
                        </div>
                    </div>
                </div>
              </div>
         </div>
 
  
    <div class="layui-form-item" style="text-align:right">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="formDemo_@randomId">@L("立即提交")</button>
            <button type="reset"  class="layui-btn layui-btn-primary addbut">@L("重置")</button>
            <button type="button" lay-filter="Close" class="layui-btn layui-btn-primary updatebut" >@L("关闭")</button>

        </div>
    </div>
</form>
 @section footer
{
<script>
    layui.use(['form', 'toast'   , 'upload', 'element' ], function () {
        var form = layui.form;
        var toast = layui.toast;
        var upload = layui.upload;
        var element = layui.element;

        console.log("*--*");

        var editId = '@editId';
        var IsUpdate = editId != "null" ? true : false;

        form.render(); // 初始化form



        $(".addbut").hide();
        $(".updatebut").hide();
        if (IsUpdate) {
            $(".updatebut").show();
            Query();
        } else {
            $(".addbut").show();
            

        }
        //监听按钮
        $(".layui-form button").click(function () {
            var id = $(this).attr('lay-filter');
            //alert(id);
            switch (id) {
                    case "Close": closeOpen(); break;
            }
        });
        var buttonEle = null;
        form.on('submit(formDemo_@randomId)', function (data) {
            console.log(data.field)
            buttonEle = button.load({ elem: this });

            //layer.msg(JSON.stringify(data.field));
            if (IsUpdate) {

                Update(data);
            } else {
                Add(data);
            }

            return false;
        });

        function Query() {
            console.log("--Query--")
            var formData = {
                Id: editId
            };
            $.ajax({
                type: 'POST',
                url: '/api/D_UserInfor/Single',//发送请求
                data: formData,
                success: function (result) {
                    //console.log(result)
                    if (result.Succeeded) {
                        form.val("edit-data_@randomId", result.Data);

                        $('#G_ImgUrlEle_@randomId').attr('src', FileHttpApiUrl+result.Data.G_ImgUrl); //图片链接（base64）
                        $('#G_ImgUrlProgressEle_@randomId').hide();

                    } else {
                        toast.error({
                            message: '@L("查询失败")',
                        });
                    }
                }
            });

        }

        function Add(data) {
            $.ajax({
                type: 'POST',
                url: '/api/D_UserInfor/Insert',//发送请求
                data: data.field,
                success: function (result) {
                    buttonEle.stop();
                    //console.log(result)
                    if (result.Succeeded) {
                        parent.layer.close(G_LayuiOpenTypeIndex);
                        toast.success({
                            message: '@L("添加成功")',
                        });
                    } else {
                        var msgError = '@L("添加失败")';
                        if (result.Errors != "") {
                            msgError = result.Errors
                        }
                        toast.error({
                            message: msgError,
                        });
                    }
                }, error: function (err) {
                    buttonEle.stop();
                }
            });

        }
        function Update(data) {
            $.ajax({
                type: 'POST',
                url: '/api/D_UserInfor/Update',//发送请求
                data: data.field,
                success: function (result) {
                    //console.log(result)
                    buttonEle.stop();

                    if (result.Succeeded) {
                        closeOpen();
                        toast.success({
                            message: '@L("修改成功")',
                        });
                    } else {
                        var msgError = '@L("修改失败")';
                        if (result.Errors != "") {
                            msgError = result.Errors
                        }
                        toast.error({
                            message: msgError,
                        });
                    }
                }, error: function (err) {
                    buttonEle.stop();
                }
            });

        }
       
        function closeOpen() {
  
            parent.layer.close(G_LayuiOpenTypeIndex);
            return true;
        }

        
//=================================上传图片Start
                //常规使用 - 普通图片上传
                var uploadG_ImgUrl = upload.render({
                    elem: '#G_ImgUrlButtonEle_@randomId'
                   // , url: '/Api/System/Uploadfiles/UserImg' //上传接口
                   , url: 'api/File/UploadFile' //上传接口
                  ,data:{
                              DirectoryName:'AutomaticCodeGeneration'
                  }
                , accept: 'images' //只允许上传图片
                , acceptMime: 'image/*' //只筛选图片
                , before: function (obj) {
                    //预读本地文件示例，不支持ie8
                    obj.preview(function (index, file, result) {
                            $('#G_ImgUrlEle_@randomId').attr('src', result); //图片链接（base64）
                    });
                        $('#G_ImgUrlProgressEle_@randomId').show();
                        element.progress('G_ImgUrlProgress_@randomId', '0%'); //进度条复位
                    layer.msg('@L("上传中")', { icon: 16, time: 0 });
                }
                , done: function (res) {
                    //如果上传失败
                    // if (res.code > 0) {
                    //     return layer.msg('上传失败');
                    // }
                    if (res.code == 200) {
                            $("#G_ImgUrlInputEle_@randomId").val(res.data)
                    } else {
                        layer.msg(res.msg);

                    }
                    //上传成功的一些操作
                    //……
                          $('#G_ImgUrlText_@randomId').html('<span style="color: green;">@L("上传成功")</span>'); //置空上传失败的状态
                       $('#G_ImgUrlProgressEle_@randomId').hide();

                }
                , error: function () {
                    //演示失败状态，并实现重传
                    var demoText = $('#ImgText_@randomId');
                    demoText.html('<span style="color: #FF5722;">@L("上传失败")</span> <a class="layui-btn layui-btn-xs demo-reload">@L("重试")</a>');
                    demoText.find('.demo-reload').on('click', function () {
                            uploadG_ImgUrl.upload();
                    });
                }
                //进度条
                , progress: function (n, elem, e) {
                      element.progress('G_ImgUrlProgress_@randomId', n + '%'); //可配合 layui 进度条元素使用
                    if (n == 100) {
                        layer.msg('@L("上传完毕")', { icon: 1 });
                    }
                }
            });

    });
</script>
}